package edu.cdnu.config;

import org.apache.shiro.session.mgt.SessionManager;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import edu.cdnu.cache.MySessionManager;
import edu.cdnu.cache.RedisSessionDao;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@ConfigurationProperties(prefix="jedis")
public class RedisAutoConfiguration {

	/**
	 * Redis服务器地址
	 */
	private String host = "127.0.0.1";
	/**
	 * 端口号
	 */
	private int port = 6379;
	/**
	 * 连接超时时间（毫秒）
	 */
	private int timeout = 10000;
	
	
	@Bean
	public JedisPoolConfig getJedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxIdle(100);
		return jedisPoolConfig;
	}
	
	@Bean
	public JedisPool getJedisPool(JedisPoolConfig jedisPoolConfig) {
		JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout);
		return jedisPool;
	}

	/**
	 * 这里配置自己的sessionManager
	 * @param redisSessionDao
	 * @return SessionManager
	 */
	@Bean
//	AbstractValidatingSessionManager
	public SessionManager getSessionManager(RedisSessionDao redisSessionDao) {
		MySessionManager sessionManager = new MySessionManager();
		sessionManager.setSessionDAO(redisSessionDao);
		//设置session过期时间
		sessionManager.setGlobalSessionTimeout(-10000000L);
		return sessionManager;
	}
	
	
	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getTimeout() {
		return timeout;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}
	
}
